package com.bianmaba.spring.security.basic;

import com.bianmaba.spring.security.handler.AjaxGlobalExceptionHandler;
import com.bianmaba.spring.security.handler.AjaxSecurityHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.web.AuthenticationEntryPoint;


@ConditionalOnClass({EnableWebSecurity.class, AuthenticationEntryPoint.class})
@ConditionalOnWebApplication
@ConditionalOnMissingBean({WebSecurityConfiguration.class})
@EnableWebSecurity//开启时将会启动自定义配置，不再使用SpringBoot的自动配置
public class BasicWebSecurityConfiguration {
    /**
     * Security相关AjaxHandler
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "security", name = "ajax", matchIfMissing = true)
    public AjaxSecurityHandler ajaxSecurityHandler() {
        return new AjaxSecurityHandler();
    }

    /**
     * 全局异常处理
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "security", name = "ajax", matchIfMissing = true)
    public AjaxGlobalExceptionHandler ajaxGlobalExceptionHandler() {
        return new AjaxGlobalExceptionHandler();
    }


    @Bean
    @ConditionalOnMissingBean
    public BasicWebSecurityConfigurerAdapter basicWebSecurityConfigurerAdapter() {
        return new BasicWebSecurityConfigurerAdapter();
    }
}
